<template>
  <div class="delivery-container">
    <!-- 搜索栏 -->
    <div class="search-wrapper">
      <el-input
          v-model="searchText"
          placeholder="配送员姓名"
          style="width: 300px"
          clearable
          @keyup.enter="fetchDeliveryList">
        <template #prefix>
          <el-icon><Search /></el-icon>
        </template>
      </el-input>
      <el-select 
        v-model="searchStatus" 
        placeholder="配送状态" 
        clearable 
        style="width: 150px">
        <el-option label="取货中" value="1" />
        <el-option label="配送中" value="2" />
        <el-option label="已完成" value="3" />
      </el-select>
      <el-button type="primary" @click="fetchDeliveryList">搜索</el-button>
      <el-button type="success" @click="handleAdd">新增派送</el-button>
    </div>

    <!-- 派送表格 -->
    <el-table :data="deliveryList" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" align="center" />
      <el-table-column prop="userName" label="配送员" width="120" align="center" />
      <el-table-column prop="address" label="配送地址" min-width="200" align="center" />
      <el-table-column prop="status" label="状态" width="100" align="center">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建人" width="120" align="center" />
      <el-table-column prop="createTime" label="创建时间" width="180" align="center" />
      <el-table-column label="操作" width="180" fixed="right" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 20px"
    />

    <!-- 表单对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新增派送' : '编辑派送'"
      width="500px"
      @close="resetForm">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="配送员" prop="userName">
          <el-input v-model="form.userName" />
        </el-form-item>
        <el-form-item label="配送地址" prop="address">
          <el-input v-model="form.address" type="textarea" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="取货中" value="1" />
            <el-option label="配送中" value="2" />
            <el-option label="已完成" value="3" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Search, Plus } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { get, post } from '@/api/api'

// 数据列表相关
const deliveryList = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const searchText = ref('')
const searchStatus = ref('')

// 表单相关
const dialogVisible = ref(false)
const dialogType = ref('add')
const formRef = ref()
const form = ref({
  id: null,
  userName: '',
  address: '',
  status: '1'
})

// 表单校验规则
const rules = {
  userName: [{ required: true, message: '请输入配送员姓名', trigger: 'blur' }],
  address: [{ required: true, message: '请输入配送地址', trigger: 'blur' }],
  status: [{ required: true, message: '请选择状态', trigger: 'change' }]
}

// 获取派送列表
const fetchDeliveryList = async () => {
  try {
    const res = await get('/delivery/page', {
      pageNum: currentPage.value,
      pageSize: pageSize.value,
      userName: searchText.value,
      status: searchStatus.value
    })
    if (res && res.data) {
      deliveryList.value = res.data.records
      total.value = res.data.total
    }
  } catch (error) {
    console.error('获取派送列表失败:', error)
    ElMessage.error('获取派送列表失败')
  }
}

// 删除派送
const handleDelete = (row) => {
  ElMessageBox.confirm('确定删除该派送记录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await post('/delivery/delete', { id: row.id })
      ElMessage.success('删除成功')
      fetchDeliveryList()
    } catch (error) {
      ElMessage.error('删除失败')
    }
  })
}

// 提交表单
const handleSubmit = async () => {
  await formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const url = dialogType.value === 'add' ? '/delivery/save' : '/delivery/update'
        await post(url, form.value)
        ElMessage.success(dialogType.value === 'add' ? '添加成功' : '更新成功')
        dialogVisible.value = false
        fetchDeliveryList()
      } catch (error) {
        ElMessage.error(dialogType.value === 'add' ? '添加失败' : '更新失败')
      }
    }
  })
}
</script> 